package net.folab.fo;

import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class ResourceUtil {

	public static ClassLoader cl = ResourceUtil.class.getClassLoader();

	public static InputStream fetchInputStream(String name) {
		String path;
		if (name.startsWith("/")) {
			path = name.substring(1);
		} else {
			path = ResourceUtil.class.getPackage().getName();
			path = path.replaceAll("\\.", "/") + "/" + name;
		}
		return cl.getResourceAsStream(path);
	}

	public static URI fetchURI(String name) {
		String path;
		if (name.startsWith("/")) {
			path = name.substring(1);
		} else {
			path = ResourceUtil.class.getPackage().getName();
			path = path.replaceAll("\\.", "/") + "/" + name;
		}
		try {
			return cl.getResource(path).toURI();
		} catch (URISyntaxException e) {
			throw new RuntimeException(e);
		}
	}

	public static URL getResource(String name) {
		return cl.getResource(name);
	}

}
